Easy
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
,
1 | 3 |
return its minimum depth = 2.
- Recursion:
很容易就想到使用递归,但是最开始时对叶子结点的判断方式错了,因为叶子结点应该是not node.right and not node.left,而不是not node。然后每次到达叶子结点就去更新一下最小的depth。
1 | class Solution: |
- BFS
1 | class Solution: |